# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.utils import timezone

# Create your models here.

class IndexBannerPic(models.Model):
    """
    首页的banner图
    """
    picture = models.CharField(max_length=200)

    class Meta:
        # managed = False
        db_table = 'smerp_banner'


class IndexNoticeMessage(models.Model):
    """
      首页的公告消息
    """
    title = models.CharField(max_length = 200)              # 标题栏
    message_text = models.TextField()                       # 公告内容
    start_time = models.DateTimeField()                     # 公告开始时间
    end_time = models.DateTimeField()                       # 公告结束时间
    create_user = models.CharField(max_length= 50)          # 公告创建人
    create_time = models.DateTimeField()                        # 公告创建时间
    message_type = models.CharField(max_length=10)          # 公告的类型

    # def __str__(self):
    #     return self.title

    # python2
    def __unicode__(self):
        return unicode(self.title)

    # 公告发布
    @property
    def was_published(self):
        return self.start_time <= timezone.now() <= self.end_time

    class Meta:
        # managed = False
        db_table = 'smerp_notices'



class Account(models.Model):
    """
        smErp的用户模型

        email varchar(32) NOT NULL注册邮箱
        erpinfo_id bigint(20) NOT NULL企业信息Id
        passwd varchar(32) NOT NULL密码
        real_name varchar(32) NOT NULL真实姓名
        phone varchar(16) NOT NULL手机号码
        address varchar(1024) NOT NULL联系地址
        id_card_no varchar(32) NOT NULL身份证号码
        is_filled tinyint(4) NOT NULL是否完善信息
        check_status tinyint(4) NOT NULL审核状态：0-初始状态，1-提交审核，2-审核通过，3-审核失败
        is_active tinyint(4) NOT NULL是否可用
        login_ip varchar(16) NOT NULL最后登录Ip
        login_time timestamp NULL最后登录时间
        create_time timestamp NULL创建时间
    """
    account_manager = models.Manager()

    # email = models.EmailField()
    # erpinfo_id = models.BigIntegerField()
    # passwd = models.CharField(max_length=32)
    # real_name = models.CharField(max_length=32)
    # phone = models.CharField(max_length=16)
    # address = models.CharField(max_length=1024)
    # id_card_no = models.CharField(max_length=32)
    # is_filled = models.SmallIntegerField()
    # check_status = models.SmallIntegerField()
    # is_active = models.SmallIntegerField()
    # login_ip = models.GenericIPAddressField()
    # login_time = models.DateTimeField()
    # create_time = models.DateTimeField()
    #
    # # class Meta:
    # #     db_table = "smErp_user"           # 给数据库表命名
    # class Meta:
    #     managed = False
    #     db_table = 'smerp_account'
    id = models.BigAutoField(primary_key=True)
    email = models.EmailField(unique=True, max_length=128)
    passwd = models.CharField(max_length=32)
    email_vaild = models.IntegerField(default=0)
    smcode_num = models.BigIntegerField(default=0)
    is_active = models.IntegerField(default=0)
    login_ip = models.CharField(max_length=16)
    login_time = models.DateTimeField(blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        # managed = False
        db_table = 'smerp_account'

#
# class Brand(models.Model):
#     """
#         smErp品牌模型
#         account_id  bigint(20) NOT NULL企业用户Id
#         name_zh     varchar(64) NOT NULL品牌中文名称
#         name_en     varchar(64) NOT NULL品牌英文名称
#         logo    varchar(128) NOT NULL商标logo
#         licence_img     varchar(128) NOT NULL品牌商标注册证书图片
#         check_status    tinyint(6) NOT NULL审核状态：0-初始状态，1-提交审核，2-审核通过，3-审核失败
#         description     varchar(1024) NOT NULL品牌描述信息
#         create_time     timestamp NOT NULL创建时间
#     """
#     brand_manage = models.Manager()
#
#     # account_id = models.BigIntegerField()
#     # name_zh = models.CharField(max_length=64)
#     # name_en = models.CharField(max_length=64)
#     # logo = models.CharField(max_length=128)
#     # license_img = models.CharField(max_length=128)
#     # check_status = models.SmallIntegerField()
#     # description = models.CharField(max_length=1024)
#     # create_time = models.DateTimeField()
#
#     id = models.BigIntegerField(primary_key=True)
#     account_id = models.BigIntegerField()
#     name_zh = models.CharField(unique=True, max_length=64)
#     name_en = models.CharField(max_length=64)
#     logo = models.CharField(max_length=128)
#     licence_img = models.CharField(max_length=128)
#     check_status = models.IntegerField()
#     description = models.CharField(max_length=1024)
#     create_time = models.DateTimeField()
#
#     class Meta:
#         managed = False
#         db_table = 'smerp_brand'



# class Product(models.Model):
#     """
#         smErp产品模型
#         id  bigint(20) unsigned NOT NULL
#         account_id  bigint(20) NOT NULL
#         brand_id    bigint(20) NOT NULL
#         name    varchar(64) NOT NULL产品名称中文
#         check_status    tinyint(4) NOT NULL审核状态：0-初始状态，1-提交审核，2-审核通过，3-审核失败
#         create_time     timestamp NOT NULL创建时间
#     """
#
#     product_manage = models.Manager()
#
#     account_id = models.BigIntegerField()
#     brand_id = models.BigIntegerField()
#     name = models.CharField(max_length=64)
#     check_status = models.SmallIntegerField()
#     create_time = models.DateTimeField()
#
#     class Meta:
#         # managed = False
#         db_table = 'smerp_product'


class Material(models.Model):
    """
    smErp原料模型

    """
    material_manage = models.Manager()

    id = models.BigAutoField(primary_key=True)
    account_id = models.BigIntegerField()
    name = models.CharField(max_length=64)
    producer = models.CharField(max_length=64)
    source_type = models.CharField(max_length=16)
    ref_image = models.CharField(max_length=128, blank=True, null=True)
    ref_url = models.CharField(max_length=128, blank=True, null=True)
    create_time = models.DateTimeField()

    class Meta:
        # managed = False
        db_table = 'smerp_material'


# class SmerpErpinfo(models.Model):
#     id = models.BigIntegerField(primary_key=True)
#     account_id = models.BigIntegerField(unique=True)
#     full_name = models.CharField(unique=True, max_length=64)
#     organization_code = models.CharField(max_length=32)
#     registration_no = models.CharField(max_length=32)
#     representative = models.CharField(max_length=32)
#     scope_normal = models.TextField()
#     scope_sepcial = models.TextField()
#     staff_num = models.IntegerField(default=0)
#     licence_img = models.CharField(max_length=200)
#     manager = models.CharField(max_length=32)
#     manager_phone = models.CharField(max_length=16)
#     manager_id_card_no = models.CharField(max_length=32)
#     manager_id_card_img = models.CharField(max_length=128)
#     check_status = models.IntegerField()
#     create_time = models.DateTimeField()
#
#     class Meta:
#         managed = False
#         db_table = 'smerp_erpinfo'

class SmerpErpinfo(models.Model):
    erpinfo_manage = models.Manager()
    id = models.BigIntegerField(primary_key=True)
    # account_id = models.BigIntegerField(unique=True)
    account = models.ForeignKey(Account)
    full_name = models.CharField(unique=True, max_length=64)
    organization_code = models.CharField(max_length=32)
    registration_no = models.CharField(max_length=32)
    representative = models.CharField(max_length=32)
    scope_normal = models.TextField()
    scope_sepcial = models.TextField()
    staff_num = models.IntegerField()
    licence_img = models.CharField(max_length=128)
    manager = models.CharField(max_length=32)
    manager_phone = models.CharField(max_length=16)
    manager_id_card_no = models.CharField(max_length=32)
    manager_id_card_img = models.CharField(max_length=200)
    check_status = models.IntegerField()
    create_time = models.DateTimeField()

    class Meta:
        # managed = False
        db_table = 'smerp_erpinfo'


class SmerpBrand(models.Model):
    # 审核状态：0-初始状态，1-待审核，2-审核通过，3-审核失败
    CHECK_STATUS_CHOICES = (
        (0, u'初始状态'),
        (1, u'待审核'),
        (2, u'审核通过'),
        (3, u'审核失败'),
    )
    brand_manage = models.Manager()

    id = models.BigIntegerField(primary_key=True)
    # account_id = models.BigIntegerField()
    account = models.ForeignKey(Account)
    name_zh = models.CharField(unique=True, max_length=64)
    name_en = models.CharField(max_length=64)
    logo = models.CharField(max_length=128)
    license_img = models.CharField(max_length=128)
    check_status = models.IntegerField(choices= CHECK_STATUS_CHOICES)
    description = models.CharField(max_length=1024)
    create_time = models.DateTimeField()

    class Meta:
        # managed = False
        db_table = 'smerp_brand'

    def license_img_as_list(self):
        return self.license_img.split(',')


class SmerpBrandLicense(models.Model):
    TYPE_CHOIES = (
        (0, u'公信力证书'),
        (1, u'信誉保证'),
        (2, u'国家驰名商标'),
        (3, u'省级著名商标'),
        (4, u'其他证书'),
    )

    # license_type_choices = (
    #
    # )
    brand_license_manage = models.Manager()
    id = models.BigAutoField(primary_key=True)
    # brand = models.BigIntegerField()
    brand = models.ForeignKey(SmerpBrand)
    name = models.CharField(max_length=32)
    type = models.IntegerField(choices=TYPE_CHOIES)
    icon = models.CharField(max_length=128)
    img = models.CharField(max_length=200)
    description = models.CharField(max_length=1024)
    create_time = models.DateTimeField()

    class Meta:
        # managed = False
        db_table = 'smerp_brand_license'


class SmerpProduct(models.Model):
    CHECK_STATUS_CHOICES = (
        (0, u'未提交'),
        (1, u'待审核'),
        (2, u'审核通过'),
        (3, u'审核失败'),
    )
    PRODUCT_TYPE_CHOICES =(
        (0, u'保健食品'),
        (1, u'功纺产品'),
        (2, u'保健用品'),
        (3, u'绿色食品'),
        (4, u'功能农产品'),
        (5, u'基地认证'),
    )
    product_manage = models.Manager()

    id = models.BigAutoField(primary_key=True)
    # account_id = models.BigIntegerField()
    account = models.ForeignKey(Account)
    # brand_id = models.BigIntegerField()
    brand = models.ForeignKey(SmerpBrand)
    name = models.CharField(max_length=64)
    type = models.IntegerField(choices=PRODUCT_TYPE_CHOICES)                            # 产品类型：0-保健食品，1-功纺产品，2-保健用品，3-绿色食品，4-功能农产品，5-基地认证
    function = models.CharField(max_length=200)             # 功能定位
    suitable = models.CharField(max_length=500)             # 使用人群
    not_suitable = models.CharField(max_length=500)         # 不适用人群
    attention = models.CharField(max_length=500)            # 注意事项
    hotline = models.CharField(max_length=100)              # 客服热线
    service_info = models.CharField(max_length=200)         # 客服信息，如客服时间等
    workflows = models.CharField(max_length=128)            # 生产流程展示
    standard = models.CharField(max_length=100)             # 生产执行标准
    standard_pic = models.CharField(max_length=200)         # 生产执行标准附件

    check_status = models.IntegerField(choices=CHECK_STATUS_CHOICES)
    create_time = models.DateTimeField()

    class Meta:
        # managed = False
        db_table = 'smerp_product'


class SmerpProductImg(models.Model):
    id = models.BigAutoField(primary_key=True)
    # product_id = models.BigIntegerField()
    product = models.ForeignKey(SmerpProduct)
    img = models.CharField(max_length=128)
    create_time = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'smerp_product_img'


class SmerpProductLicense(models.Model):
    TYPE_CHOIES = (
        ("0", u'公信力证书'),
        ("1", u'信誉保证'),
        ("2", u'国家驰名商标'),
        ("3", u'省级著名商标'),
        ("4", u'其他证书'),
    )
    id = models.BigAutoField(primary_key=True)
    product = models.ForeignKey(SmerpProduct, db_column="product_id", to_field='id')
    name = models.CharField(max_length=32)
    type = models.CharField(max_length=32, choices=TYPE_CHOIES)
    img = models.CharField(max_length=128)
    effect_time = models.DateTimeField(blank=True, null=True)
    expire_time = models.DateTimeField(blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'smerp_product_license'


class SmerpMaterial(models.Model):
    material_manage = models.Manager()
    id = models.BigAutoField(primary_key=True)
    # account_id = models.BigIntegerField()
    account = models.ForeignKey(Account)
    name = models.CharField(max_length=64)
    producer = models.CharField(max_length=64)
    source_type = models.CharField(max_length=16)
    ref_image = models.CharField(max_length=128, blank=True, null=True)
    ref_url = models.CharField(max_length=128, blank=True, null=True)
    create_time = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'smerp_material'


class SmerpProductMaterial(models.Model):
    product_material_manage = models.Manager()
    id = models.BigAutoField(primary_key=True)
    # product_id = models.BigIntegerField()
    product = models.ForeignKey(SmerpProduct)
    # material_id = models.BigIntegerField()
    material = models.ForeignKey(SmerpMaterial)
    class Meta:
        managed = False
        db_table = 'smerp_product_material'


class SmerpSmcodeOrder(models.Model):
    sm_order_manage = models.Manager()

    id = models.BigIntegerField(primary_key=True)
    order_id = models.CharField(unique=True, max_length=32)

    # account_id = models.BigIntegerField()
    account = models.ForeignKey(Account, db_column="account_id", related_name="smerpsmcodeorders", to_field='id')
    product = models.ForeignKey(SmerpProduct, db_column="product_id", related_name="smerpsmcodeorders", to_field='id')

    num = models.BigIntegerField()
    goods_desc = models.CharField(max_length=64)
    goods_amount = models.BigIntegerField()
    pay_type = models.CharField(max_length=32)
    pay_status = models.IntegerField()
    pay_time = models.DateTimeField(blank=True, null=True)
    bank_order_id = models.CharField(max_length=64, blank=True, null=True)
    add_code_status = models.IntegerField()
    ext = models.CharField(max_length=512)
    create_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'smerp_smcode_order'



class SmerpSmcode(models.Model):
    smcode_manage = models.Manager()
    id = models.BigAutoField(primary_key=True)
    # order_id = models.CharField(unique=True, max_length=64)
    smerpsmcodeorder = models.OneToOneField(SmerpSmcodeOrder, db_column="order_id", related_name="smerpsmcode", to_field='order_id')

    # account_id = models.BigIntegerField()
    smerpaccount = models.ForeignKey(Account, db_column="account_id", related_name="smerpaccountcodes", to_field='id')

    # product_id = models.BigIntegerField()
    smerpproduct = models.ForeignKey(SmerpProduct, db_column="product_id", related_name="smerpproductcodes", to_field='id')

    num = models.CharField(max_length=255)
    start_no = models.BigIntegerField()
    end_no = models.BigIntegerField()
    publish_num = models.BigIntegerField()
    publish_end_no = models.BigIntegerField()
    create_time = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'smerp_smcode'


class SmerpPublishSmcodeLog(models.Model):
    sm_publish_manage = models.Manager()
    id = models.BigAutoField(primary_key=True)
    # account_id = models.BigIntegerField()
    smerpaccount = models.ForeignKey(Account, db_column="account_id", related_name="smerppublishsmcodelogs", to_field='id')

    # product_id = models.BigIntegerField()
    smerpproduct = models.ForeignKey(SmerpProduct, db_column="product_id", related_name="smerppublishsmcodelogs", to_field='id')

    num = models.BigIntegerField()
    start_no = models.BigIntegerField()
    end_no = models.BigIntegerField()
    price = models.FloatField()
    province = models.CharField(max_length=64)
    create_time = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'smerp_publish_smcode_log'



class WxmpScanLog(models.Model):
    wxmp_scan_log_manage = models.Manager()

    id = models.BigAutoField(primary_key=True)
    # product_id = models.BigIntegerField()
    product = models.ForeignKey(SmerpProduct, db_column="product_id", related_name="smscanlogproduct_id", to_field="id")

    smcode_no = models.BigIntegerField()
    product_name = models.CharField(max_length=64)
    buyer_id = models.BigIntegerField()
    buyer_name = models.CharField(max_length=32)
    publish_price = models.FloatField()
    publish_province = models.CharField(max_length=64)
    publish_time = models.DateTimeField(blank=True, null=True)
    scan_province = models.CharField(max_length=64)
    scan_city = models.CharField(max_length=64)
    scan_location = models.CharField(max_length=256)
    scan_ptype = models.CharField(max_length=64)
    scan_os = models.CharField(max_length=32)
    scan_browser = models.CharField(max_length=64)
    scan_ip = models.CharField(max_length=32)
    create_time = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'wxmp_scan_log'


class WxmpAbnormalLog(models.Model):
    wxmp_abnormal_manage = models.Manager()
    # 异常类型，too_manay_scan：过多扫描，more_area_scan：多地区扫描，买家反馈：buyer_feedback
    ABNORMAL_TYPES = (
        ("too_manay_scan", u"过多扫描"),
        ("more_area_scan", u"多地区扫描"),
        ("buyer_feedback", u"买家反馈"),
    )
    id = models.BigAutoField(primary_key=True)
    # product_id = models.BigIntegerField()
    product = models.ForeignKey(SmerpProduct, db_column="product_id", to_field="id")

    smcode_no = models.BigIntegerField()
    scan_count = models.IntegerField()
    type = models.CharField(max_length=32, choices=ABNORMAL_TYPES)
    mark = models.CharField(max_length=128, blank=True, null=True)
    create_time = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'wxmp_abnormal_log'
        # unique_together = (('product', 'smcode_no'),)


class WxmpFeedback(models.Model):
    wxmp_feedback_manage = models.Manager()

    id = models.BigAutoField(primary_key=True)
    buyer_id = models.BigIntegerField()
    buyer_name = models.CharField(max_length=32)
    # product_id = models.BigIntegerField()
    product = models.ForeignKey(SmerpProduct, db_column="product_id", to_field="id")

    type = models.CharField(max_length=32)
    content = models.CharField(max_length=512)
    create_time = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'wxmp_feedback'


class SmplatConfig(models.Model):
    smplatconfig_manage = models.Manager()
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=64)
    value = models.TextField(blank=True, null=True)
    vtype = models.CharField(max_length=16)
    label = models.CharField(max_length=64)
    description = models.TextField(blank=True, null=True)
    input_options = models.TextField()
    update_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'smplat_config'